//
//  Screen.swift
//  Calculator
//
//  Created by 白瑾浩 on 2022/1/12.
//

import UIKit

class Screen: UIView {
    var inputLabel:UILabel?,historyLabel:UILabel?
    var figureArray:Array<Character> = ["0","1","2","3","4","5","6","7","8","9","."]
    var funcArray = ["+","-","*","/","%","^"]
    var inputString = ""
    var historyString = ""
    init(  ){
        inputLabel = UILabel()
        historyLabel = UILabel()
        super.init(frame: CGRect.zero)
        
        installUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func installUI(){
        self.backgroundColor = UIColor.white
        
        inputLabel?.textAlignment = .right
        historyLabel?.textAlignment = .right
        
        inputLabel?.font = UIFont.systemFont(ofSize: 34)
        historyLabel?.font = UIFont.systemFont(ofSize: 26)
        
        inputLabel?.textColor = UIColor.orange
        historyLabel?.textColor = UIColor.black
        
        inputLabel?.adjustsFontSizeToFitWidth = true //字数根据宽度适配
        inputLabel?.minimumScaleFactor = 0.5
        historyLabel?.adjustsFontSizeToFitWidth = true
        historyLabel?.minimumScaleFactor = 0.5
        
        inputLabel?.lineBreakMode = .byTruncatingHead
        historyLabel?.lineBreakMode = .byTruncatingHead
        
        
        inputLabel?.numberOfLines = 0
        historyLabel?.numberOfLines = 0
        
        self.addSubview( inputLabel! )
        self.addSubview( historyLabel! )
        
        inputLabel?.snp.makeConstraints({ make in
            make.left.equalTo( 10 )
            make.right.equalTo( -10 )
            make.bottom.equalTo( -10 )
            make.height.equalTo( inputLabel!.superview!.snp.height ).multipliedBy( 0.5 ).offset( -10 )
        })
        
        historyLabel?.snp.makeConstraints({ make in
            make.left.equalTo( 10 )
            make.right.equalTo( -10 )
            make.top.equalTo( -10 )
            make.height.equalTo( historyLabel!.superview!.snp.height ).multipliedBy( 0.5 ).offset( -10 )
        })
//
//        inputLabel?.text = "0"
//        historyLabel?.text = "0"
//
        
        
    }
    
    func inputContent(content:String){
        if !figureArray.contains(content.last!) && !funcArray.contains(content){
            print("数据不合格")
            return
        }
        
        if inputString.count > 0{
            //数字后可以输入任何字符
            if( figureArray.contains( inputString.last! ) ){
                inputString.append( content )
                inputLabel?.text = inputString
            }else{
                //运算符只能输入数字
                if figureArray.contains( content.last! ){
                    inputString.append( content )
                    inputLabel?.text = inputString
                }
            }
        }else{
            //只有数字才能做为首字母
            if figureArray.contains( content.last! ){
                inputString.append( content )
                inputLabel?.text = inputString
            }
        }
        
        
    }
    func refreshHistory(){
        
        historyString = inputString
        historyLabel?.text = historyString 
        
    }
    
    func clearContent(){
        inputString = ""
        inputLabel?.text = inputString
    }
    
    func deleteInput(){
        if inputString.count > 0 {
            inputString.remove(at: inputString.index( before:  inputString.endIndex ) )
            inputLabel?.text = inputString
        }
    }

}
